<?php


/**
* Puree CMS
* http://www.molotsoft.ru
*
* @package Puree_CMS
* @subpackage Admin/Behaviors
* @author Ivan Chistyakov <ivan@molotsoft.ru>
* @version $Id: field_type_time.php 1039 2010-03-23 13:46:46Z molotsoft@gmail.com $
* @link $HeadURL: http://puree-cms.googlecode.com/svn/trunk/plugins/admin/models/behaviors/field_type_time.php $
*/


class FieldTypeTimeBehavior extends ModelBehavior {

/******************************************************************************/

var $settings = array();

/******************************************************************************/

function setup(&$Model, $settings = array()) {
	$this->settings[$Model->name] = (array)$settings;
}

/******************************************************************************/

function _currentTime(&$Model) {
	return date('H:i:s');
}

function _strictTypeTime(&$Model, $value) {
	return (bool)preg_match('~^(\d{2})\:(\d{2})\:(\d{2})$~', $value) ? $value : $this->_currentTime($Model);
}

/******************************************************************************/

function beforeSave(&$Model) {
	$creating = empty($Model->id) && (!isset($Model->data[$Model->name][$Model->primaryKey]) || empty($Model->data[$Model->name][$Model->primaryKey]));
	foreach ($this->settings[$Model->name] as $field_name => $field_params) {
		if ($Model->hasField($field_name) && ($Model->getFieldType($field_name) === 'time')) {
			if (isset($Model->data[$Model->name][$field_name])) {
				$Model->data[$Model->name][$field_name] = $this->_strictTypeTime($Model, $Model->data[$Model->name][$field_name]);
			} elseif ($creating && (!isset($Model->data[$Model->name][$field_name]) || empty($Model->data[$Model->name][$field_name]))) {
				$Model->data[$Model->name][$field_name] = $this->_currentTime($Model);
			}
		}
	}
}

/******************************************************************************/

}


?>